package temp;

/**
 * @description:
 * @author: zk
 * @date: 2024-02-01
 */
public class Test {
    public static void main(String[] args) {
//        ListNode n1 = new ListNode(5, null);
//        ListNode n2 = new ListNode(4, n1);
//        ListNode t = new ListNode(3, n2);
//        ListNode n3 = new ListNode(2, t);
//        ListNode head = new ListNode(1, n3);
//        //head.next = head;
//        //ListNode node = reverseList(head);
////        System.out.println(node);
        System.out.println(11 % 10);
    }

    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        if (l2 == null) {
            return l1;
        }

        ListNode zero = new ListNode(0);
        ListNode head = null;
        ListNode tail = null;
        int up = 0;// 进位标识
        while ((l1 != zero || l2 != zero)) {
            // 计数是否有进位的情况
            int sum = l1.val + l2.val + up;
            if (sum >= 10) {
                up = 1;
                // 取出个位
                sum = sum % 10;
            } else {
                up = 0;
            }

            // 尾插法
            ListNode node = new ListNode(sum);
            if (tail == null) {
                head = node;
            } else {
                tail.next = node;
            }
            tail = node;

            l1 = l1.next == null ? zero : l1.next;
            l2 = l2.next == null ? zero : l2.next;
        }

        // 最后进位的情况
        if (up > 0) {
            tail.next = new ListNode(up);
        }
        return head;
    }


    public ListNode removeElements(ListNode head, int val) {
        ListNode newHead = new ListNode(-val,head);
        ListNode pre = newHead;
        while (pre.next != null) {
            // 删除
            if (pre.next.val == val) {
                pre.next = pre.next.next;
            } else {
                pre = pre.next;
            }
        }
        return newHead.next;
    }
    public static ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        ListNode pa = headA;
        ListNode pb = headB;
        while (pa !=  pb) {
            pa = pa == null ? headB : pa.next;
            pb = pb == null ? headA : pb.next;
        }
        return pa;
    }

    static class ListNode {
        int val;
        ListNode next;

        ListNode() {
        }

        ListNode(int val) {
            this.val = val;
        }

        ListNode(int val, ListNode next) {
            this.val = val;
            this.next = next;
        }

        public int getVal() {
            return val;
        }
    }
}
